<?php

/**
 * 用户关注
 */

namespace app\api\model;


use app\common\lib\RespCode;
use custom\Token;
use PDOStatement;
use think\Collection;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Model;

class AgentAttention {

    /**
     * 添加用户关注
     * @param $params
     * @return int|string
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function add($params) {
        if ($params['r_type'] == 2) throw new Exception('未开放小区关注');
        $house = db("house")
            ->where('is_shelf', 1)
            ->where('deleted', 0)
            ->find($params['id']);
        if (empty($house)) throw new Exception('房源已下架');
        $agent_attention = db("recommend_house")
            ->where('house_id', $params['id'])
            ->where('agent_id', $params['agent_id'])
            ->find();
        if (!empty($agent_attention)) throw new Exception('请勿重复添加');
//        if (db('recommend_house')
//                ->where('agent_id', $params['agent_id'])
//                ->count() >= 10) {
//            throw new Exception('最多添加10个关注');
//        }
        $hv_name=db('house_village')->where("id={$house['village_id']}")->value('name');
        $hv_name=$hv_name??'';
        if ($house['bedroom']&&$house['livingroom']){
            $boor=$house['bedroom'].'室'.$house['livingroom'].'厅';
        }else{
            $boor='';
        }
        $insert = [
            'agent_id' => $params['agent_id'],
            'house_id' => $params['id'],
            'name' => $hv_name?$hv_name.' '.$boor:$house['title'],
            'desc' => '',
            'itime' => time(),
            'utime' => time(),
            'is_modify' =>0//房源标题是否被编辑(默认未编辑)， 1已编辑 0未编辑


        ];
        $res = db('recommend_house')->insert($insert);
        if (!$res) {
            throw new Exception('操作失败');
        }
        return true;
    }

    /**
     * 删除关注
     * @param $params
     * @return int
     * @throws Exception
     * @throws PDOException
     */
    public static function del($params) {
        if (isset($params['ids'])&&$params['ids']){
            $ids = $params['ids'];
            $ids = explode(',', $ids);
            Db::name('recommend_house')->where('agent_id', $params['agent_id'])->where('house_id','in', $ids)->delete();
        }

    }

    /**
     * 关注房源
     * @param $params
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function show($params) {
        if ($params['r_type'] == 1) {
            $map = [];
            if (!empty($params['title'])) {
                $map[] = ['h.title', 'like', '%' . trim($params['title']) . '%'];
            }
            $House = new House();
            $column = array_merge($House->houseFields, $House->villageFields, $House->typeFields, $House->auctionFields,$House->businessFields,$House->reginFields);
            $pure_id = db('recommend_house')
                ->where('agent_id', $params['agent_id'])
                ->order('id', 'desc')
                ->column('house_id');
            $map[] = ['h.id', 'in', $pure_id];
            $pure_id_str = join(',', $pure_id);
            $res = $House->house_list($map, '', $column, 1, 10, [], $pure_id_str)[0];
            return House::house_list_style($res);
        } else {
            return [];
        }
    }

    /**
     * 经纪人是否关注这个房源
     * @param $params
     * @return array|PDOStatement|string|Model|null
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function isAttention($params) {
        return db('recommend_house')
            ->where('agent_id', $params['agent_id'])
            ->where('house_id', $params['id'])
            ->find();
    }


}
